package com.xii.eduservice.controller;


import com.xii.commonutils.R;
import com.xii.eduservice.entity.EduCourse;
import com.xii.eduservice.entity.vo.CourseInfoForm;
import com.xii.eduservice.entity.vo.CoursePublishVo;
import com.xii.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程管理 前端控制器
 * </p>
 *
 * @author xhjava
 * @since 2022-02-13
 */
@RestController
@Api(description="课程管理")
@RequestMapping("/eduservice/educourse")
public class EduCourseController {

    //注入接口
    @Autowired
    private EduCourseService eduCourseService;

    /**
     * @description: 查询所有课程
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @GetMapping("queryall")
    public R queryAll(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    //分页
//    @ApiOperation(value = "分页")
//    @GetMapping("queryall/{current}/{limit}")
//    public R pageListTeacher(@PathVariable long current,  //当前页
//                             @PathVariable long limit) {   //每页记录数
//        //创建page对象
//        Page<EduCourse> pagecourse = new Page<>(current, limit);
//        //调用方法实现分页
//        //调用方法 底层封装 会把所有数据封装到pageteacher中
//        eduCourseService.page(pagecourse, null);
//        long total = pagecourse.getTotal(); //总记录数
//        List<EduCourse> records = pagecourse.getRecords(); //每页数据的集合
//        return R.ok().data("total", total).data("list", records);
//    }


    @PostMapping("saveCourse")
    @ApiOperation(value = "新增课程")
    public R saveCourse(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm){        //使用封装好的类 CourseInfoForm  可以同时添加描述

        String id = eduCourseService.saveCourseInfo(courseInfoForm);
        //返回添加课程的id
        return R.ok().data("id",id);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("courseinfo/{courseid}")
    public R getById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String courseid){
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfoFormById(courseid);
        return R.ok().data("item", courseInfoForm);
    }


    @ApiOperation(value = "更新课程")
    @PostMapping("updatecourseinfo")
    public R updateCourseInfoById(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm){
        eduCourseService.updateCourseInfoById(courseInfoForm);
        return R.ok();
    }
    
    
    //根据课程id 查询课程确认信息
    @ApiOperation(value = "根据课程id 查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVos = eduCourseService.getPublishCourseInfoFormById(courseId);
        return R.ok().data("publishcourse",coursePublishVos);
    }

    //最终状态的发布
    @PostMapping("publicCourse/{id}")
    public R publicCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }
}

